package com.huaxin.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.huaxin.common.R;
import com.huaxin.common.media.ImageGalleryActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/3/25.
 */
public class ImageGroupView extends FrameLayout implements View.OnClickListener {

    private final Context mContext;
    ImageView lefttop;
    ImageView righttop;
    ImageView leftbottom;
    ImageView rightbottom;
    private static final int dividerwidth = 4;//px
    private int halfwidth = -1;
    private int halfheight = -1;
    private int width,height;
    private FrameLayout.LayoutParams params1;
    private FrameLayout.LayoutParams params2;
    private FrameLayout.LayoutParams params3;
    private FrameLayout.LayoutParams params4;
    private int imgCount;
    private String[] urls;
    private List<String> urlList;

    public ImageGroupView(Context context) {
        super(context);
        mContext = context ;
        init();
    }

    public ImageGroupView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context ;
        init();
    }

    public ImageGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context ;
        init();

    }

    protected void init() {

        lefttop = new ImageView(getContext());
        righttop = new ImageView(getContext());
        leftbottom = new ImageView(getContext());
        rightbottom = new ImageView(getContext());
        lefttop.setScaleType(ImageView.ScaleType.CENTER_CROP);
        righttop.setScaleType(ImageView.ScaleType.CENTER_CROP);
        leftbottom.setScaleType(ImageView.ScaleType.CENTER_CROP);
        rightbottom.setScaleType(ImageView.ScaleType.CENTER_CROP);
        params1 = new LayoutParams(-1,-1, Gravity.LEFT|Gravity.TOP);
        params2 = new LayoutParams(-1,-1, Gravity.RIGHT|Gravity.TOP);
        params3 = new LayoutParams(-1,-1, Gravity.LEFT|Gravity.BOTTOM);
        params4 = new LayoutParams(-1,-1, Gravity.RIGHT|Gravity.BOTTOM);
        addView(lefttop,params1);
        addView(righttop,params2);
        addView(leftbottom,params3);
        addView(rightbottom,params4);
        lefttop.setOnClickListener(this);
        righttop.setOnClickListener(this);
        leftbottom.setOnClickListener(this);
        rightbottom.setOnClickListener(this);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        resizeChild(widthMeasureSpec,heightMeasureSpec);
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    }

    public void resizeChild(int widthMeasureSpec, int heightMeasureSpec){
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        halfheight = (height-dividerwidth)/2;
        halfwidth = (width-dividerwidth)/2;
        switch (imgCount){
            case 0:
            case 1:
                params1.width = FrameLayout.LayoutParams.MATCH_PARENT;
                params1.height = FrameLayout.LayoutParams.MATCH_PARENT;
                lefttop.setLayoutParams(params1);
                break;
            case 2:
                params1.width = halfwidth;
                params1.height = FrameLayout.LayoutParams.MATCH_PARENT;
                lefttop.setLayoutParams(params1);
                params2.width = halfwidth;
                params2.height = FrameLayout.LayoutParams.MATCH_PARENT;
                righttop.setLayoutParams(params2);
                break;
            case 3:
                params1.width = halfwidth;
                params1.height = FrameLayout.LayoutParams.MATCH_PARENT;
                lefttop.setLayoutParams(params1);
                params2.width = halfwidth;
                params2.height = halfheight;
                righttop.setLayoutParams(params2);
                params4.width = halfwidth;
                params4.height = halfheight;
                rightbottom.setLayoutParams(params4);
                break;
            case 4:
                params1.width = halfwidth;
                params1.height = halfheight;
                lefttop.setLayoutParams(params1);
                params2.width = halfwidth;
                params2.height = halfheight;
                righttop.setLayoutParams(params2);
                params3.width = halfwidth;
                params3.height = halfheight;
                leftbottom.setLayoutParams(params3);
                params4.width = halfwidth;
                params4.height = halfheight;
                rightbottom.setLayoutParams(params4);
                break;
            default:
                break;
        }
    }


    public void setImageUrl(String[] url) {
        this.urls =url;
        imgCount = url.length;
        switch (imgCount){
            case 0:
                lefttop.setImageResource(R.drawable.icon_no_pic);
                break;
            case 1:
                loadImg(lefttop,url[0]);
                break;
            case 2:
                loadImg(lefttop,url[0]);
                loadImg(righttop,url[1]);
                break;
            case 3:
                loadImg(lefttop,url[0]);
                loadImg(righttop,url[1]);
                loadImg(rightbottom,url[2]);
                break;
            default:
                loadImg(lefttop,url[0]);
                loadImg(righttop,url[1]);
                loadImg(rightbottom,url[2]);
                loadImg(leftbottom,url[3]);
                break;
        }

    }
    public void setImageUrlList(List<String> url) {
        this.urlList =url;
        imgCount = url.size();
        switch (imgCount){
            case 0:
                lefttop.setImageResource(R.drawable.icon_no_pic);
                break;
            case 1:
                loadImg(lefttop,url.get(0));
                break;
            case 2:
                loadImg(lefttop,url.get(0));
                loadImg(righttop,url.get(1));
                break;
            case 3:
                loadImg(lefttop,url.get(0));
                loadImg(righttop,url.get(1));
                loadImg(rightbottom,url.get(2));
                break;
            default:
                loadImg(lefttop,url.get(0));
                loadImg(righttop,url.get(1));
                loadImg(rightbottom,url.get(2));
                loadImg(leftbottom,url.get(3));
                break;
        }

    }
    private void loadImg(ImageView lefttop, String s) {
        lefttop.setBackgroundResource(R.color.md_gay_300);
        Glide.with(mContext).load(s).into(lefttop);
    }


    public void setImageUrl(ArrayList<String> url) {
        setImageUrlList( url);
    }

    @Override
    public void onClick(View v) {
        int index = 0;
        if (v.equals(lefttop)) {
            index=0;
        } else if (v.equals(righttop)) {
            index=1;
        } else if (v.equals(leftbottom)) {
            index=2;
        } else if (v.equals(rightbottom)) {
            index = imgCount==3?2:3;
        }
        ImageGalleryActivity.show(getContext(), urls, index, true);
//        ImageGalleryActivity.show(getContext(), urlList, index, true);
    }


}
